package com.qf.web.controller;

import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * projectName:myxiaomi
 * author:lcy
 * time:2024/4/17 11:05
 * description:
 */
@WebServlet("/cartservlet")
public class Cartservlet extends BaseServlet {
    //http://localhost:8080/cartservlet?method=addCart&goodsId=1&number=1
    public String addCart(HttpServletRequest request, HttpServletResponse response){
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");

        User user= (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }

        if(StringUtil.isEmpty(goodsId)){
            request.setAttribute("msg", "商品id为空");
            return "/message.jsp";
        }

        int num=1;
        if(!StringUtil.isEmpty(number)){
            try {
                num=Integer.parseInt(number);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        try {
            GoodsService goodsService=new GoodsServiceImpl();
            int goods_id=Integer.parseInt(goodsId);
            Goods goods=goodsService.selectById(goods_id);
            CartService cartService=new CartServiceImpl();
            Cart cart=cartService.select(user.getId(),goods_id);

            if(cart!=null){
                cart.setNum(cart.getNum()+num);
                cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                cartService.update(cart);
            }else {
                cart=new Cart(user.getId(),goods_id,num,goods.getPrice());
                cartService.insert(cart);
            }

            return "redirect:/cartSuccess.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "添加购物车失败："+e.getMessage());
            return "message.jsp";
        }
    }

    //http://localhost:8080/cartservlet?method=getCart
    public String getCart(HttpServletRequest request, HttpServletResponse response){
        User user= (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }

        try {
            CartService cartService=new CartServiceImpl();
            List<Cart> cartList= cartService.selectByUserId(user.getId());
            request.setAttribute("cartList",cartList);
            return "/cart.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询购物车失败："+e.getMessage());
            return "message.jsp";
        }
    }

    //cartservlet?method=addCartAjax&goodsId="+pid+"&number="+num
    public String addCartAjax(HttpServletRequest request, HttpServletResponse response){
        User user=(User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }

        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");

        if(StringUtil.isEmpty(goodsId)){
            request.setAttribute("msg", "商品id不能为空");
            return "/message.jsp";
        }

        try {
            int num= Integer.parseInt(number);
            int goods_id= Integer.parseInt(goodsId);
            CartService cartService=new CartServiceImpl();
            if(num==0){
                cartService.delete(user.getId(),goods_id);
            }else if (num==1||num==-1){
                Cart cart = cartService.select(user.getId(), goods_id);
                if (cart != null) {
                    GoodsService goodsService=new GoodsServiceImpl();
                    Goods goods = goodsService.selectById(goods_id);
                    cart.setNum(cart.getNum()+num);
                    cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                    cartService.update(cart);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "更新购物车失败："+e.getMessage());
            return "/message.jsp";
        }
        return null;
    }

    //cartservlet?method=clearCartAjax",
    public String clearCartAjax(HttpServletRequest request, HttpServletResponse response){
        User user= (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }

        try {
            CartService cartService=new CartServiceImpl();
            cartService.clearAll(user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","清空失败");
            return "/message.jsp";
        }
        return null;
    }


}


